﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ChudeComponent.BLL;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace ChudeComponent.DAL
{
    public class ChudeDAL:IChudeDAL
    {
        #region IChudeDAL connection
        private string _connectionstring = Commom.WCManager.ConnectionString();

        public string ConnectionString
        {
            get { return _connectionstring; }
            set { _connectionstring = value; }
        }
        #endregion

        #region IChude Method
        #region Get
        public void Get(Chude chude)
        {
            try
            {
                using (SqlConnection con = new SqlConnection(this.ConnectionString))
                {
                    SqlParameter[] par = new SqlParameter[1];
                    par[0] = new SqlParameter("@chu_de_id", chude.Chu_de_id);
                    using(SqlDataReader dr=SqlHelper.ExecuteReader(con,CommandType.StoredProcedure,"P_chu_de_Get",par))
                    {
                        while(dr.Read())
                        {
                            chude.Chu_de_id = int.Parse(dr["chu_de_id"].ToString());
                            chude.Ung_dung_id = int.Parse(dr["ung_dung_id"].ToString());
                            chude.Ma = dr["ma"].ToString();
                            chude.Ten = dr["ten"].ToString();
                            chude.Mieuta = dr["mieuta"].ToString();
                            chude.Icon = dr["icon"].ToString();
                            chude.Thutu = int.Parse(dr["thutu"].ToString());
                        }

                    }
                }
            }
            catch (Exception ex)
            { }
        }
        #endregion

        #region Add

        public int Add(Chude chude)
        {
            int RowNo=-1;
            try
            {
                using (SqlConnection con = new SqlConnection(this.ConnectionString))
                {
                    SqlParameter[] par = new SqlParameter[7];
                    par[0] = new SqlParameter("@chu_de_id", chude.Chu_de_id);
                    par[0].Direction = ParameterDirection.Output;
                    par[1] = new SqlParameter("@ung_dung_id", chude.Ung_dung_id);
                    par[2] = new SqlParameter("@ma", chude.Ma);
                    par[3] = new SqlParameter("@ten", chude.Ten);
                    par[4] = new SqlParameter("@mieuta", chude.Mieuta);
                    par[5] = new SqlParameter("@icon", chude.Icon);
                    par[6] = new SqlParameter("@thutu", chude.Thutu);
                    SqlHelper.ExecuteNonQuery(con, CommandType.StoredProcedure, "P_chu_de_Insert", par);
                    RowNo = int.Parse(par[0].ToString());
                }
                
            }
            catch (Exception ex)
            {
            }
            return RowNo;
        }
        #endregion

        #region Update
        public void Update(Chude chude)
        {
            try
            {
                using (SqlConnection con = new SqlConnection(this.ConnectionString))
                {
                    SqlParameter[] par = new SqlParameter[7];
                    par[0] = new SqlParameter("@chu_de_id", chude.Chu_de_id);
                    par[1] = new SqlParameter("@ung_dung_id", chude.Ung_dung_id);
                    par[2] = new SqlParameter("@ma", chude.Ma);
                    par[3] = new SqlParameter("@ten", chude.Ten);
                    par[4] = new SqlParameter("@mieuta", chude.Mieuta);
                    par[5] = new SqlParameter("@icon", chude.Icon);
                    par[6] = new SqlParameter("@thutu", chude.Thutu);
                    SqlHelper.ExecuteNonQuery(con, CommandType.StoredProcedure, "P_chu_de_Update", par);
                }
            }
            catch (Exception ex) { }
        }
        #endregion
        #region Delete
        public void Delete(Chude chude)
        {
            try
            {
                using (SqlConnection con = new SqlConnection(this.ConnectionString))
                {
                    SqlParameter[] par = new SqlParameter[1];
                    par[0] = new SqlParameter("@chu_de_id", chude.Chu_de_id);
                    SqlHelper.ExecuteNonQuery(con, CommandType.StoredProcedure, "P_chu_de_Delete", par);
                }
            }
            catch (Exception ex)
            { }
        }
        #endregion
        #region GetAll
        public List<Chude> GetAll(int currentPage, int pageSize, out int total, string ten, string column_sort, string type_sort)
        {
            List<Chude> solist = new List<Chude>();
            try
            {
                
                using (SqlConnection con = new SqlConnection(this.ConnectionString))
                {
                    SqlParameter[] par = new SqlParameter[6];
                    par[0] = new SqlParameter("@currentPage", currentPage);
                    par[1] = new SqlParameter("@pagSize", pageSize);
                    par[2] = new SqlParameter("@total", SqlDbType.Int);
                    par[2].Direction = ParameterDirection.Output;
                    par[3] = new SqlParameter("@ten", ten);
                    par[4] = new SqlParameter("@column_sort", column_sort);
                    par[5] = new SqlParameter("@type_sort", type_sort);

                    using (SqlDataReader dr = SqlHelper.ExecuteReader(con, CommandType.StoredProcedure, "P_chu_de_GetAll", par))
                    {
                        Chude chude = new Chude();
                        chude.Chu_de_id = int.Parse(dr["chu_de_id"].ToString());
                        chude.Ung_dung_id = int.Parse(dr["ung_dung_id"].ToString());
                        chude.Ma = dr["ma"].ToString();
                        chude.Ten = dr["ten"].ToString();
                        chude.Mieuta = dr["mieuta"].ToString();
                        chude.Icon = dr["icon"].ToString();
                        chude.Thutu = int.Parse(dr["thutu"].ToString());
                    }
                    total = int.Parse(par[2].Value.ToString());

                }
                return solist;
            }
            catch (Exception ex)
            { throw; }
            
        }
        #endregion
        #endregion

    }
}
